package com.demo.bookstore.servlet;

import com.demo.bookstore.bean.Book;
import com.demo.bookstore.bean.Cart;
import com.demo.bookstore.service.BookService;
import com.demo.bookstore.service.CartService;
import com.demo.bookstore.service.impl.BookServiceImpl;
import com.demo.bookstore.service.impl.CartServiceImpl;
import com.demo.bookstore.vo.SysResult;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;

/**
 * @author ddone
 * @date 2024/3/16-09:47
 */
@WebServlet("/cart")
public class CartServlet extends BaseServlet {
    private final BookService bookService = new BookServiceImpl();
    private final CartService cartService = new CartServiceImpl();
    private final ObjectMapper MAPPER = new ObjectMapper();

    public void toCart(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        this.processTemplate("cart/cart", req, resp);
    }

    /**
     * 添加购物车的时候 要看下有没有购物车,从session中获取
     * 如果没有购物车 需要新建一个购物车
     *
     * @param req
     * @param resp
     */
    public void addCart(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bookId = req.getParameter("bookId");
        Cart cart = getCart(req, resp);
        Book book = bookService.findBookById(bookId);
        // 添加完成之后 返回购物车Item的数量
        int itemCount = cartService.addCart(cart, book);
        req.getSession().setAttribute("cart", cart);
        // 添加完毕之后 返回购物车的数量
        String result = MAPPER.writeValueAsString(SysResult.success(itemCount));
        resp.getWriter().write(result);

    }

    public Cart getCart(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        return (Cart) Optional
                .ofNullable(req.getSession().getAttribute("cart"))
                .orElse(new Cart());
    }

    public void getCartItems(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Cart cart = getCart(req, resp);
        cartService.setCartItems(cart);
        String result = MAPPER.writeValueAsString(SysResult.success(cart));
        resp.getWriter().write(result);
    }

    /**
     * 按照BookID 找到Item 然后更新Count
     *
     * @param req
     * @param resp
     */
    public void updateCount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bookId = req.getParameter("bookId");
        String count = req.getParameter("count");
        Cart cart = getCart(req, resp);
        cartService.updateCount(cart, bookId, count);
        resp.getWriter().write(
                MAPPER.writeValueAsString(SysResult.success(""))
        );
    }

    public void deleteItemById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Cart cart = getCart(req, resp);
        String bookId = req.getParameter("bookId");
        cartService.deleteItemByBookId(cart, bookId);
        resp.getWriter().write(
                MAPPER.writeValueAsString(SysResult.success(""))
        );
    }

    public void clearCart(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        req.getSession().removeAttribute("cart");
        resp.getWriter().write(
                MAPPER.writeValueAsString(SysResult.success(""))
        );
    }

}
